using System;
using System.Collections.Generic;
using System.IO;

using octalforty.Componento.SemanticModel;

namespace octalforty.Componento.ComponentModel.Packaging
{
    public class PackageRepositoryDefinitionParser
    {
        public RepositoryEntry[] ParseRepositoryDefinition(StreamReader streamReader)
        {
            List<RepositoryEntry> entries = new List<RepositoryEntry>();

            string line;
            while((line = streamReader.ReadLine()) != null)
            {
                line = line.Trim();
                if(line.StartsWith("//") || line == "")
                    continue;

                entries.Add(ParseRepositoryEntry(line));
            } // while

            return entries.ToArray();
        }

        private RepositoryEntry ParseRepositoryEntry(string line)
        {
            string[] parts = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            if(parts.Length != 3)
                // TODO: Better exception
                throw new ApplicationException("Invalid component directory format");

            return new RepositoryEntry(parts[0], new Version(parts[1]), new Uri(parts[2], UriKind.Absolute));
        }
    }
}
